package com.xdf.xzymanagementsystem.service.impl;

import com.xdf.xzymanagementsystem.dao.LogSalesorderdtlfinishedMapper;
import com.xdf.xzymanagementsystem.dao.SysUserMapper;
import com.xdf.xzymanagementsystem.entity.LogSalesorderdtlfinished;
import com.xdf.xzymanagementsystem.entity.SysUser;
import com.xdf.xzymanagementsystem.service.LogSalesorderdtlfinishedService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class LogSalesorderdtlfinishedServiceImpl implements LogSalesorderdtlfinishedService {
    @Autowired
    private LogSalesorderdtlfinishedMapper dao;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 2021年12月3日 13:58:20 chanchaw
     * 填充当前登录用户名称之后写入数据库
     * @param record
     * @return
     */
    @Override
    public int insert(LogSalesorderdtlfinished record) {
        record = Optional.ofNullable(record).orElse(new LogSalesorderdtlfinished());
        record.setUser(getUsername());

        return dao.insertSelective(record);
    }

    @Override
    public int insert(Long pk, int result, String remark) {
        LogSalesorderdtlfinished record = new LogSalesorderdtlfinished();
        record.setPk(pk);
        record.setResult(result);
        record.setRemark(remark);
        return insert(record);
    }

    @Override
    public int insert(Long pk, String remark) {
        return insert(pk,1,remark);
    }

    /**
     * 2021年12月3日 13:51:45 chanchaw
     * 获取当前登录的用户 = sys_user.id
     * 账号 8888 的 id = 18
     * @return
     */
    @Override
    public Integer getUserId() {
        Integer userPk = Integer.parseInt((String) SecurityUtils.getSubject().getPrincipal());
        return userPk;
    }

    /**
     * 2021年12月3日 15:42:44 chanchaw
     * 参数 Map 类型的两个 key:
     * pks = List<Integer>
     * record = log_salesorderdtlfinished 对象
     * @param params
     * @return
     */
    @Override
    public int multiInsert(Map<String,Object> params) {
        LogSalesorderdtlfinished record = (LogSalesorderdtlfinished)params.get("record");
        record.setUser(getUsername());
        return dao.multiInsert(params);
    }


    // 返回当前登录系统的用户名称：管理员2，一厂材料
    public String getUsername(){
        Integer userId = getUserId();
        SysUser user = sysUserMapper.selectByPrimaryKey(userId);
        return user.getUserName();
    }
}
